﻿
Imports Oracle.DataAccess.Client

Public Class fListaOrdenCompra
    Dim lTipodoc As String
    Dim lNumero As Integer
    Public ReadOnly Property Ptipodoc() As String
        Get
            Return lTipodoc
        End Get
    End Property
    Public ReadOnly Property Pnumero() As Integer
        Get
            Return lNumero
        End Get
    End Property
    Private Function TipoEstado(ByVal Estado As String) As Integer
        Select Case Estado
            Case "Activas"
                Return 1
            Case "Inactivas"
                Return 2
            Case "Cargadas"
                Return 3
            Case Else
                Return 0
        End Select
    End Function
    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        Dim qOrder = " ORDER BY NUMERO"
        cmd.CommandText = "SELECT * FROM COM_ENCABEZADOORDENCOMPRA "
        Dim qWhere As String = "WHERE "
        Dim CuentaW As Int16 = 0
        If Me.dtpFechaInicial.Text.Trim.Length > 0 Then
            CuentaW += 1
            qWhere &= "FECHA >= '" & FormatDateTime(Me.dtpFechaInicial.Value.Date, DateFormat.ShortDate) & "'"
        End If
        If Me.dtpFechaFinal.Text.Trim.Length > 0 Then
            If CuentaW > 0 Then _
               qWhere &= " AND "
            CuentaW += 1
            'qWhere &= "FECHA <= TO_DATE('26012009','DDMMYYYY')"
            qWhere &= "FECHA <= '" & FormatDateTime(Me.dtpFechaFinal.Value.Date, DateFormat.ShortDate) & "'"
        End If
        If Me.txtProveedor.Text.Trim.Length Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "IDPROVEEDOR =" & Me.txtProveedor.Text & ""
        End If
        If Me.cbEstado.Text <> "Todas" Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "ESTADOORDEN ='" & TipoEstado(Me.cbEstado.Text) & "'"
        End If
        If CuentaW = 0 Then _
            qWhere = ""
        cmd.CommandText &= qWhere & qOrder
        Try
            Conn.Open()
            DsCompras.COM_ENCABEZADOORDENCOMPRA.Rows.Clear()
            DsCompras.COM_ENCABEZADOORDENCOMPRA.Load(cmd.ExecuteReader)
            Conn.Close()
            COM_ENCABEZADOORDENCOMPRADataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub fListaOrdenCompra_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.cbEstado.Text = "Todas"
        dtpFechaInicial.Value = dtpFechaFinal.Value.AddDays(-30)
    End Sub

    Private Sub btnBuscaProveedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaProveedor.Click
        Dim fLProveedor As New fBuscaTercero
        If fLProveedor.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtProveedor.Text = fLProveedor.midTercero
        End If
    End Sub

    Private Sub AsignaOrdenCompra(ByVal Fila As Integer)
        If Fila >= 0 AndAlso COM_ENCABEZADOORDENCOMPRADataGridView.Rows(Fila).Cells("ESTADOORDEN").Value = 1 Then
            lTipodoc = COM_ENCABEZADOORDENCOMPRADataGridView.Rows(Fila).Cells("TIPODOC").Value
            lNumero = COM_ENCABEZADOORDENCOMPRADataGridView.Rows(Fila).Cells("NUMERO").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            MessageBox.Show("La orden de compra ya fue cargada o esta inactiva", "ordencompra", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub COM_ENCABEZADOORDENCOMPRADataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles COM_ENCABEZADOORDENCOMPRADataGridView.CellMouseDoubleClick
        AsignaOrdenCompra(e.RowIndex)
    End Sub

    Private Sub COM_ENCABEZADOORDENCOMPRADataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles COM_ENCABEZADOORDENCOMPRADataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaOrdenCompra(COM_ENCABEZADOORDENCOMPRADataGridView.CurrentRow.Index)
        End If
    End Sub
End Class